﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AuctionScanWin.Properties;

using LuaInterface;

namespace AuctionScanWin
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Settings.Default.AuctioneerFile))
            {
                SelectLuaFile();
            }
            LoadLuaFile();
        }

        private void auctioneerluaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectLuaFile();
            LoadLuaFile();
        }

        private void SelectLuaFile()
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;

            Lua lua = new Lua();
            try
            {
                lua.DoFile(openFileDialog1.FileName);
                Settings.Default.AuctioneerFile = openFileDialog1.FileName;
                Settings.Default.Save();
            }
            catch (LuaException)
            {
                MessageBox.Show("Arquivo Lua inválido");
                SelectLuaFile();
            }
            catch (Exception exc)
            {
                MessageBox.Show("Erro ao abrir arquivo: " + exc.Message);
            }
            finally
            {
                lua.Dispose();
            }

        }

        Lua lua = new Lua();
        LuaTable config;
        LuaTable itemDB;
        LuaTable snapshotDB;
        LuaTable historyDB;
        LuaTable fixedPriceDB;
        LuaTable transactionDB;

        LuaTable scanData;

        private void LoadLuaFile()
        {
            if (string.IsNullOrEmpty(Settings.Default.AuctioneerFile)) return;

            lua.DoFile(Settings.Default.AuctioneerFile);

            scanData = lua.GetTable("AucScanData");

            config = lua.GetTable("AuctionConfig");
            itemDB = lua.GetTable("AuctioneerItemDB");
            snapshotDB = lua.GetTable("AuctioneerSnapshotDB");
            historyDB = lua.GetTable("AuctioneerHistoryDB");
            fixedPriceDB = lua.GetTable("AuctioneerFixedPriceDB");
            transactionDB = lua.GetTable("AuctioneerTransactionDB");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            treeView1.Visible = false;
            BindSource(scanData);
            treeView1.Visible = true;
        }

        private void BindSource(LuaTable table)
        {
            treeView1.Nodes.Clear();            
            AddNodes(table, null);
            treeView1.ExpandAll();
        }

        void AddNodes(LuaTable table, TreeNode parent)
        {
            TreeNodeCollection nodes = parent == null ? treeView1.Nodes : parent.Nodes;
            foreach (DictionaryEntry e in table)
            {
                if (e.Value is LuaTable)
                {
                    TreeNode node = nodes.Add(e.Key.ToString());
                    AddNodes(e.Value as LuaTable, node);
                }
                else
                {
                    if (e.Key.ToString() == "image")
                    {
                        string command = e.Value.ToString().Replace("return ", "ScanData = ");
                        lua.DoString(command);
                        LuaTable scan = lua.GetTable("ScanData");

                        AddNodes(scan, parent);
                    }
                    else
                    {
                        nodes.Add(string.Format("{0}: {1}", e.Key, e.Value));
                    }
                }
            }       
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BindSource(itemDB);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BindSource(snapshotDB);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            BindSource(historyDB);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            BindSource(fixedPriceDB);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            BindSource(transactionDB);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Settings.Default.AuctioneerFile);
        }


    }
}
